package com.foreknow.view;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;
import com.foreknow.util.PropertyJTable;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MainWindow  {
    private JButton btnNewButton_1;
    private JButton btnNewButton_2;
    private JButton btnNewButton_3;
    private JFrame frame;
    private JPanel contentPane;
    GuestbookServiceImpl  service = new GuestbookServiceImpl();
    JTable table;
    String[] columnTitle = {"id" , "name" , "phone","email","title","content","createdtime"};
    //DefaultTableModel工具类
    PropertyJTable  propertyJTable=PropertyJTable.getInstance();


    /**
     * Create the frame.
     */
    public MainWindow() {
        frame = new JFrame();
        frame.setBounds(100, 100, 600, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setLocationRelativeTo(null);

        //删除
        btnNewButton_2 = new JButton("delete");
        btnNewButton_2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                /**
                 * 1.得到选中的行数
                 * 2.获取选中行的id
                 * 3.判断是否删除成功
                 * 4.成功就把该行remove掉
                 */

                System.out.println(table.getRowCount());
                System.out.println(table.getSelectedRow());
                int res = JOptionPane.showConfirmDialog(null, "确认删除吗！", "提示",
                        JOptionPane.YES_NO_OPTION);
                if (res == JOptionPane.YES_OPTION) {
                    int row = table.getSelectedRow();
                    if (row == -1){
                        row = table.getRowCount() - 1;
                    }
                    int id = (Integer) table.getValueAt(row, 0);
                    //System.out.println(id);
                    boolean isRight = service.delete(id);
                    if (isRight) {
                        System.out.println("删除成功");
                        List<Object> list = service.getQueryAll();
                        //1. 创建一个二维数组
                        Object[][] td = new Object[list.size()][7];
                        //2. 给二维数组添加数据 (遍历List集合)
                        for(int i = 0;i<list.size();i++){
                            Guestbook gb = (Guestbook) list.get(i);
                            td[i][0] = gb.getId();
                            td[i][1] = gb.getName();
                            td[i][2] = gb.getPhone();
                            td[i][3] = gb.getEmail();
                            td[i][4] = gb.getTitle();
                            td[i][5] = gb.getContent();
                            td[i][6] = gb.getCreatedtime();
                        }
                        propertyJTable.update(td,columnTitle);
                    }else
                    {
                        System.out.println("删除失败");
                    }
                }
            }
        });


        btnNewButton_2.setBounds(162, 215, 97, 23);
        frame.getContentPane().add(btnNewButton_2);

        //初始化表格数据
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.getQueryAll();
        //需要将list转换成一个二维数组tableData，之后就能在表格中显示数据了
        //1. 创建一个二维数组
        Object[][] tableData = new Object[list.size()][7];
        //2. 给二维数组添加数据 (遍历List集合)
        for(int i = 0;i<list.size();i++){
            Guestbook gb = (Guestbook) list.get(i);
            tableData[i][0] = gb.getId();
            tableData[i][1] = gb.getName();
            tableData[i][2] = gb.getPhone();
            tableData[i][3] = gb.getEmail();
            tableData[i][4] = gb.getTitle();
            tableData[i][5] = gb.getContent();
            tableData[i][6] = gb.getCreatedtime();
        }

        table = propertyJTable.createJTable(tableData,columnTitle);
        table.setBounds(43, 61, 338, 129);
        JScrollPane jsp = new JScrollPane(table);
        jsp.setBounds(43,51,338,129);
        //jsp.setSize(300,300);
        frame.add(jsp);
        frame.setVisible(true);
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        MainWindow frame = new MainWindow();
//
    }
}
